package make

import (
	"github.com/spf13/cobra"
	"wskyygyso/api/database/migrations"
	"wskyygyso/api/pkg/migrate"
)

var CmdMigrate = &cobra.Command{
	Use:   "migrate",
	Short: "Run database migration",
}

var CmdUp = &cobra.Command{
	Use:   "up",
	Short: "Run unmigrated migrations",
	Run:   runUp,
}

var CmdDown = &cobra.Command{
	Use:   "down",
	Short: "Down migrations",
	Run:   runDown,
}

var CmdReset = &cobra.Command{
	Use:   "reset",
	Short: "reset migrations",
	Run:   runReset,
}

var CmdRefresh = &cobra.Command{
	Use:   "refresh",
	Short: "refresh migrations",
	Run:   runRefresh,
}

var CmdFresh = &cobra.Command{
	Use:   "fresh",
	Short: "fresh migrations",
	Run:   runFresh,
}

func init() {
	CmdMigrate.AddCommand(
		CmdUp,
		CmdDown,
		CmdReset,
		CmdRefresh,
		CmdFresh,
	)
}

func migrator() *migrate.Migrator {
	migrations.Initialize()
	return migrate.NewMigrator()
}

func runUp(cmd *cobra.Command, args []string) {
	migrator().Up()
}

func runDown(cmd *cobra.Command, args []string) {
	migrator().Rollback()
}

func runReset(cmd *cobra.Command, args []string) {
	migrator().Reset()
}

func runRefresh(cmd *cobra.Command, args []string) {
	migrator().Refresh()
}

func runFresh(cmd *cobra.Command, args []string) {
	migrator().Fresh()
}
